package com.timoon.component.panel.nav;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.wicket.markup.html.panel.Panel;

import com.timoon.component.panel.common.DefaultEmptyPanel;
import com.timoon.sample.dao.model.Resource;

/**
 * 每一个有资源的面板或page都持有该类的一个对象<br>
 * 对象里currentParentResource表明当前面板的父资源<br>
 * 若currentParentResource为null表明当前为顶级面板
 * @author Administrator
 *
 */
public class ResourceMapper implements java.io.Serializable{
	private static final long serialVersionUID = 5549988737266988413L;
	
	/**
	 * 当前用户所有的资源
	 * 
	 * 自己code-自己实例
	 */
	private Map<String,Resource> resourceMap=new HashMap<String,Resource>();
	
	private Map<Integer,String> idCodeMap=new HashMap<Integer,String>();
	
	/**
	 * 父code--子code集合
	 */
	private Map<String,List<String>> childCodeMap=new HashMap<String,List<String>>();
	
	/**
	 *注册当前用户的所有资源 
	 * @param resources
	 */
	public void registResource(List<Resource> resources){
		
		if(resources!=null){
			for(Resource resource:resources){
				this.idCodeMap.put(resource.getId(), resource.getCode());
			}
			
			for(Resource resource:resources){
				
				/*将code提出来这侧resourceMap*/
				this.resourceMap.put(resource.getCode(), resource);
				
				/**/
				String pcode=this.idCodeMap.get(resource.getpId());
				List<String> childCode=this.childCodeMap.get(pcode);
				if(childCode==null){
					childCode=new ArrayList<String>();
				}
				childCode.add(resource.getCode());
				
				this.childCodeMap.put(pcode, childCode);
			}
			
		}
		
		
	}
	
	/**
	 * 获取子code集合
	 * @param code
	 * @return
	 */
	public List<String> getChildCodeByCode(String code){
		return this.childCodeMap.get(code);
	}
	/**
	 * 通过code获取面板实例
	 * @param code
	 * @param panelId
	 * @return
	 */
	public Panel getPanelByCode(String code, String panelId) {
		// TODO Auto-generated method stub
		String panelName=this.resourceMap.containsKey(code)?this.resourceMap.get(code).getPanel():"com.timoon.component.panel.common.DefaultEmptyPanel";
		panelName=(panelName==null?"com.timoon.component.panel.common.DefaultEmptyPanel":panelName);
		try {
			Class<?> bean=Class.forName(panelName);
			
			try {
				Constructor<?> con=bean.getConstructor(String.class);
				
				try {
					Panel panel=(Panel) con.newInstance(panelId);
					
					return panel;
				} catch (InstantiationException | IllegalAccessException
						| IllegalArgumentException | InvocationTargetException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
				
			} catch (NoSuchMethodException | SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		
		return new DefaultEmptyPanel(panelId);
	}
	
	public Map<String, Resource> getResourceMap() {
		return resourceMap;
	}
	public void setResourceMap(Map<String, Resource> resourceMap) {
		this.resourceMap = resourceMap;
	}
}
